// component/gui/region/region.js
const leftPad0 = function (v, n) {
  if (!v) {
    v = "";
  }
  let prefix = "";
  for (let i = 0; i < n; i++) {
    prefix += "0";
  }
  return (prefix + v).substr(-n);
};
const stringToDate = function (str) {
  str = str.replace(/-/g, "/");
  return new Date(str);
};
const isLeapYear = function (year) {
  if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
    return true;
  }
  return false;
};
const now = new Date();
const years = [];
const beginYear = 2020;
for (var i = beginYear; i <= now.getFullYear()+100; i++) {
  years.push(i + "年");
}
const months = [];
for (var i = 0; i < 12; i++) {
  months.push(leftPad0(i + 1, 2) + "月");
}
const days = [];
for (var i = 0; i < 31; i++) {
  days.push(leftPad0(i + 1, 2) + "日");
}
Component({
  externalClasses: ['g-class'],
  /**
   * 组件的属性列表
   */
  properties: {
    state: {
      type: Boolean,
      value: false
    },
    //联级，1可选年, 2可选年月, 3可选年月日
    level: {
      type: Number,
      value: 3
    },

    //初始化值，
    value: {
      type: Object,
      value: [] //可以是省市区对应的ID || 名称
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isscroll1: true,
    isscroll2: true,
    isscroll3: true,
    actData: [0, 0, 0], //当前活跃索引值
    jsonData: {
      years,
      months,
      days
    },
    yearsType: 0,
    params: {
      years,
      months,
      days
    }
  },

  ready() {
    const me = this;
    const {
      level,
      value,
      actData,
      jsonData
    } = me.data;

  },

  /**
   * 组件的方法列表
   */
  methods: {
    //点选
    inChange(e) {
      const me = this;
      const getData = e.currentTarget.dataset;
      let months = me.data.jsonData.months;
      let days = me.data.jsonData.days;
      let setAct;
      switch (Number(getData.type)) {
        //年
        case 1:
          console.log(e.currentTarget)
          setAct = 'actData[0]';
          me.setData({
            'actData[0]': getData.index,
            'actData[1]': 0,
            'isscroll1': false,
            'isscroll2': true,
            yearsType: e.currentTarget.dataset.index
          });
          if (me.data.level == 3) {
            me.setData({
              'actData[2]': 0,
              'isscroll3': true
            });
          }
          break;
          //月
        case 2:
          me.setData({
            'actData[1]': getData.index,
            'isscroll2': false,
          });
          if (me.data.level == 3) {
            me.setData({
              'actData[2]': 0,
              'isscroll3': true,
            });
          }
          me.setDays(e.currentTarget.dataset.index)
          break;

          //日
        case 3:
          //setAct = 'actData[2]';
          me.setData({
            'actData[2]': getData.index,
            'isscroll3': false,
          })
          break;
      }
    },

    setDays(e) {
      var me = this;
      console.log(me.data.yearsType)
      let days = me.data.jsonData.days = [];
      var yearsType = me.data.yearsType;
      let daysLength = 31;
      switch (e + 1) {
        case 2:
          var type = Number(me.data.jsonData.years[yearsType].split('年')[0]);
          console.log(type)
          if (type % 4 === 0) {
            daysLength = 29;
          } else {
            daysLength = 28;
          }

          break;
        case 4:
        case 6:
        case 9:
        case 11:
          daysLength = 30;
          break;
        default:
          daysLength = 31;
          break;
      }
      for (var i = 0; i < daysLength; i++) {
        if(i <= 9 && i != 0){
          days.push('0' + i + "日");
        }else{
          days.push(i  + "日");
        }
        
      }
      me.setData({
        jsonData: me.data.jsonData,
      })
    },

    //确定
    areaInEnter() {
      const me = this;
      var jsonData = me.data.jsonData;
      var actData = me.data.actData;
     me.setData({
      params: {
        years : jsonData.years[actData[0]],
        months : jsonData.months[actData[1]],
        days : jsonData.days[actData[2]]
      },
     })
     me.triggerEvent('areaInEnter', me.data.params);
    },
    timeReset(e) {
      const that = this;
      that.triggerEvent('timeReset', that.data.params)
    },
    //取消
    inCancel() {
      var that = this;
      that.setData({
        params: {
          years : '',
          months : '',
          days : ''
        },
        'actData[0]': 0,
        'isscroll1': false,
        'actData[1]': 0,
        'isscroll2': false,
        'actData[2]': 0,
        'isscroll3': false,
      })
      console.log(that.data.actData)
    }
  }
})